﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Acme.BookStore.ShoppingCartServer;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.ShoppingCarts
{
    /// <summary>
    /// 购物车
    /// </summary>
    [ApiExplorerSettings(GroupName = "v2")]
    public class ShoppingCartServices : ApplicationService, IShoppingCartServices
    {
        IRepository<ShoppingCart> repository;
        IRepository<GoodsInfor> GetRepository;

        public ShoppingCartServices(IRepository<ShoppingCart> repository, IRepository<GoodsInfor> GetRepository)
        {
            this.repository = repository;
            this.GetRepository = GetRepository;
        }


        /// <summary>
        /// 购物车添加
        /// </summary>
        /// <param name="shoppingCartDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> InsertShoppingCart(ShoppingCartDTO shoppingCartDTO)
        {
            var list= await repository.GetListAsync(x => x.GoodsId == shoppingCartDTO.GoodsId && x.UserId == shoppingCartDTO.UserId&&x.IsDeleted==false);
            if (list.Count > 0)
            {
                list.First().GoodsNum += shoppingCartDTO.GoodsNum;
                await repository.UpdateAsync(list.First());
                return ApiResult.Ok();
            }
            var entity = ObjectMapper.Map<ShoppingCartDTO, ShoppingCart>(shoppingCartDTO);
            await repository.InsertAsync(entity);
            return ApiResult.Ok();
        }
        /// <summary>
        /// 购物车显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("ShowShoppingCart")]
        public async Task<ApiResult> ShowShoppingCart(Guid id)
        {
            var entity = await repository.GetListAsync(x => x.UserId == id);
            var list= await GetRepository.GetListAsync();
            var listcar = from a in entity
                          join b in list
                          on a.GoodsId equals b.Id
                          select new ShoppingCartrequestDTO
                          {
                              Id= a.Id,
                              Name = b.Name,
                              SalesPrice = b.SalesPrice,
                              ProductImages = b.ProductImages,
                              GoodsNum = a.GoodsNum
                          };
            return ApiResult.Ok(listcar.ToList());
        }
        /// <summary>
        /// 修改购物车商品数量
        /// </summary>
        /// <param name="updateSumDTO"></param>
        /// <returns></returns>
        [HttpPut("UpdateSumAsync")]
        public async Task<ApiResult> UpdateSumAsync(UpdateSumDTO updateSumDTO)
        {
            var entity = await repository.GetListAsync(x => x.Id == updateSumDTO.Id);
            entity.FirstOrDefault().GoodsNum = updateSumDTO.Sums;
            await repository.UpdateAsync(entity.FirstOrDefault());
            return ApiResult.Ok();
        }
        /// <summary>
        /// 移除当前商品购物车
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeleteShoppingCart")]
        public async  Task<ApiResult> DeleteShoppingCart(Guid id)
        {
            await repository.DeleteAsync(x=>x.Id==id);
            return ApiResult.Ok();
        }


    }
}
